package com.crape.faq.bo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.crape.account.bo.IAccountBO;
import com.crape.common.enums.AnswerAcceptedType;
import com.crape.common.enums.AnswerRefType;
import com.crape.common.enums.IsMeType;
import com.crape.common.exceptions.BOException;
import com.crape.common.model.AccountInfo;
import com.crape.common.model.Answer;
import com.crape.common.thread.ThreadObjManager;
import com.crape.faq.bo.IAnswerBO;
import com.crape.faq.dao.IAnswerDAO;
import com.crape.faq.dao.IQuestionDAO;

public class AnswerBOImpl implements IAnswerBO {

	private IQuestionDAO questionDAOImpl;
	private IAnswerDAO answerDAOImpl;
	private IAccountBO accountBOImpl;

	@Autowired
	public void setquestionDAOImpl(IQuestionDAO questionDAOImpl) {
		this.questionDAOImpl = questionDAOImpl;
	}

	@Autowired
	public void setanswerDAOImpl(IAnswerDAO answerDAOImpl) {
		this.answerDAOImpl = answerDAOImpl;
	}

	@Autowired
	public void setAccountBOImpl(IAccountBO accountBOImpl) {
		this.accountBOImpl = accountBOImpl;
	}

	@Override
	public List<Answer> getPagedAnswers(Long questionid, Integer startIndex, Integer pageSize) throws BOException {
		if (questionid == null || startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		List<Answer> answers = answerDAOImpl.getPagedAnswers(questionid, startIndex, pageSize);
		if (answers == null || answers.isEmpty()) {
			return null;
		}
		Long accountid = ThreadObjManager.getLoginUser().getAccountid();
		for (Answer answer : answers) {
			AccountInfo accountInfo = accountBOImpl.getAccountInfoById(answer.getAccountid());
			if (accountInfo != null) {
				answer.setAccountInfo(accountInfo);
			}
			if(accountid!=null&&accountid==answer.getAccountid()){
				answer.setIsMe(IsMeType.YES.getCode());
			}else{
				answer.setIsMe(IsMeType.NO.getCode());
			}
			List<AccountInfo> refAccounts = accountBOImpl.getPagedAccountByAnswerRef(answer.getId(), AnswerRefType.zan.getCode(), 0, 6);
			answer.setRefAccounts(refAccounts);
		}
		return answers;
	}
	
	@Override
	public Answer getTheAnswer(Long questionid) throws BOException {
		if(questionid==null){
			return null;
		}
		Answer answer = answerDAOImpl.getTheAnswer(questionid);
		if(answer==null){
			return null;
		}
		AccountInfo accountInfo = accountBOImpl.getAccountInfoById(answer.getAccountid());
		if (accountInfo != null) {
			answer.setAccountInfo(accountInfo);
		}
		return answer;
	}

	@Override
	public void answerQuestion(Long accountid, Long questionid, String answer) throws BOException {
		if (accountid == null||questionid==null||answer==null) {
			return;
		}
		answerDAOImpl.insertAnswer(accountid, questionid, answer);
		Long answerNum = answerDAOImpl.countAnswers(questionid);
		questionDAOImpl.updateQuestionAnswers(questionid, answerNum);
	}
	
	@Override
	public void updateAnswer(Answer answer) throws BOException {
		if(answer==null){
			return ;
		}
		answerDAOImpl.updateAnswer(answer);
	}
	
	@Override
	public void setAnswerOfQuestion(Long questionid, Long answerid,Boolean isAnswer) throws BOException {
		if (questionid == null || answerid == null || isAnswer == null) {
			return;
		}
		Answer answer = new Answer(); 
		answer.setQuestionid(questionid);
		answer.setAccepted(AnswerAcceptedType.NO.getCode());
		answerDAOImpl.updateAnswer(answer);
		if(isAnswer){
			Answer answer2 = new Answer(); 
			answer2.setId(answerid);
			answer2.setAccepted(AnswerAcceptedType.YES.getCode());
			answerDAOImpl.updateAnswer(answer2);
		}
	}

	@Override
	public void deleteAnswerById(Long id) throws BOException {
		if (id == null) {
			return;
		}
		answerDAOImpl.deleteAnswerById(id);
	}

	@Override
	public Long addAnswersRef(Long accountid, Long answerid, Integer type) throws BOException {
		if (answerid == null || type == null) {
			return 0L;
		}
		Long num = answerDAOImpl.countAnswerRefByAccountid(accountid,answerid);
		if(num<1){
			answerDAOImpl.insertAnswerRef(accountid, answerid, type);
		}
		Long refNum = answerDAOImpl.countAnswerRef(answerid, type);
		Answer answer = new Answer();
		answer.setId(answerid);
		if (type == AnswerRefType.cai.getCode()) {
			answer.setCai(refNum);
			answerDAOImpl.updateAnswer(answer);
		} else if (type == AnswerRefType.zan.getCode()) {
			answer.setZan(refNum);
			answerDAOImpl.updateAnswer(answer);
		}
		return refNum;
	}
	
}
